home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / screen-resolution-extra / policyui.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  8.2 KB  |  240 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import gtk
  5. import gobject
  6. import sys
  7. import dbus
  8. import logging
  9. import ScreenResolution
  10. from ScreenResolution.policykit import PolicyKitAuthentication
  11. from ScreenResolution import ui
  12. POLICY_KIT_ACTION = 'com.ubuntu.screenresolution.mechanism.configure'
  13. SERVICE_NAME = 'com.ubuntu.ScreenResolution.Mechanism'
  14. OBJECT_PATH = '/'
  15. INTERFACE_NAME = 'com.ubuntu.ScreenResolution.Mechanism'
  16. usage = 'python policyui.py 1024x768'
  17. import os
  18. import sys
  19. from subprocess import Popen, PIPE
  20. import XKit
  21. from XKit import xutils, xorgparser
  22. clean = False
  23.  
  24. def checkVirtual(virtres):
  25.     source = '/etc/X11/xorg.conf'
  26.     
  27.     try:
  28.         a = xutils.XUtils(source)
  29.     except (IOError, XKit.xorgparser.ParseException):
  30.         return False
  31.  
  32.     if len(a.globaldict['Screen']) > 0:
  33.         res = None
  34.         for screen in a.globaldict['Screen']:
  35.             
  36.             try:
  37.                 res = a.getValue('SubSection', 'virtual', 0, identifier = 'Display', sect = 'Screen', reference = None)
  38.                 if res:
  39.                     if 'x' in res.lower():
  40.                         res = res.lower().split('x')
  41.                     elif ' ' in res.lower().strip():
  42.                         res = res.lower().split(' ')
  43.                         res = filter((lambda x: x != ''), res)
  44.                         if len(res) == 2 and int(virtres[0]) <= int(res[0]) and int(virtres[1]) <= int(res[1]):
  45.                             return True
  46.                     
  47.             continue
  48.             except (XKit.xorgparser.SectionException, XKit.xorgparser.OptionException, AttributeError):
  49.                 continue
  50.             
  51.  
  52.         
  53.     
  54.     return False
  55.  
  56.  
  57. def computeVirtual(*params):
  58.     '''
  59.     Compute the virtual resolution on the basis of the
  60.     position and resolution of the screens.
  61.     '''
  62.     xResolutions = []
  63.     yResolutions = []
  64.     for param in params[0]:
  65.         position = param[:str(param).find(':')].split(',')
  66.         relativeResolution = param[param.find(':') + 1:].split('x')
  67.         xres = int(position[0]) + int(relativeResolution[0])
  68.         yres = int(position[1]) + int(relativeResolution[1])
  69.         xResolutions.append(xres)
  70.         yResolutions.append(yres)
  71.     
  72.     return (max(xResolutions), max(yResolutions))
  73.  
  74.  
  75. def compareFrameBuffer(virtual):
  76.     x = virtual[0]
  77.     y = virtual[1]
  78.     p1 = Popen([
  79.         'xrandr',
  80.         '-q'], stdout = PIPE)
  81.     p = p1.communicate()[0]
  82.     a = p.split('\n')
  83.     b = a[0].split(',')
  84.     c = b[2].strip().split(' ')
  85.     c.remove('maximum')
  86.     c.remove('x')
  87.     xfb = int(c[0])
  88.     yfb = int(c[1])
  89.     if x > xfb or y > yfb:
  90.         return [
  91.             True,
  92.             (str(x), str(y))]
  93.     return [
  94.         False]
  95.  
  96.  
  97. def get_xkit_service(widget = None):
  98.     '''
  99.     returns a dbus interface to the screenresolution mechanism
  100.     '''
  101.     policy_auth = PolicyKitAuthentication()
  102.     granted = policy_auth.obtain_authorization(POLICY_KIT_ACTION, widget)
  103.     logging.debug('granted = %s' % granted)
  104.     if not granted:
  105.         return None
  106.     service_object = dbus.SystemBus().get_object(SERVICE_NAME, OBJECT_PATH)
  107.     service = dbus.Interface(service_object, INTERFACE_NAME)
  108.     return service
  109.  
  110.  
  111. def gui_dialog(message, parent_dialog, message_type = None, widget = None, page = 0, broken_widget = None):
  112.     '''
  113.     Displays an error dialog.
  114.     '''
  115.     if message_type == 'error':
  116.         message_type = gtk.MESSAGE_ERROR
  117.         logging.error(message)
  118.     elif message_type == 'info':
  119.         message_type = gtk.MESSAGE_INFO
  120.         logging.info(message)
  121.     
  122.     dialog = gtk.MessageDialog(parent_dialog, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, message_type, gtk.BUTTONS_OK, message)
  123.     translation = ui.AbstractUI()
  124.     dialog.set_title(translation.string_title)
  125.     if widget != None:
  126.         if isinstance(widget, gtk.CList):
  127.             widget.select_row(page, 0)
  128.         elif isinstance(widget, gtk.Notebook):
  129.             widget.set_current_page(page)
  130.         
  131.     
  132.     if broken_widget != None:
  133.         broken_widget.grab_focus()
  134.         if isinstance(broken_widget, gtk.Entry):
  135.             broken_widget.select_region(0, -1)
  136.         
  137.     
  138.     if parent_dialog:
  139.         dialog.set_position(gtk.WIN_POS_CENTER_ON_PARENT)
  140.         dialog.set_transient_for(parent_dialog)
  141.     else:
  142.         dialog.set_position(gtk.WIN_POS_CENTER)
  143.     ret = dialog.run()
  144.     dialog.destroy()
  145.     return ret
  146.  
  147.  
  148. class BootWindow:
  149.     
  150.     def __init__(self, resolution):
  151.         translation = ui.AbstractUI()
  152.         self.permission_text = translation.string_permission_text
  153.         self.dbus_cant_connect = translation.string_dbus_cant_connect
  154.         self.operation_complete = translation.string_operation_complete
  155.         self.cant_apply_settings = translation.string_cant_apply_settings
  156.         self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
  157.         self.window.connect('delete_event', self.on_delete_event)
  158.         self.window.connect('destroy', self.on_destroy)
  159.         self.window.set_border_width(20)
  160.         self.window.set_title(translation.string_title)
  161.         self.window.set_position(gtk.WIN_POS_CENTER)
  162.         gtk.window_set_default_icon_from_file('/usr/share/icons/hicolor/16x16/apps/gnome-display-properties.png')
  163.         vbox = gtk.VBox(spacing = 20)
  164.         self.resolution = resolution
  165.         self.label = gtk.Label(self.permission_text)
  166.         self.label.set_line_wrap(True)
  167.         self.label.set_justify(gtk.JUSTIFY_FILL)
  168.         self.button1 = gtk.Button(label = None, stock = 'gtk-yes', use_underline = False)
  169.         self.button1.connect('clicked', self.on_button1_clicked, None)
  170.         self.button1.show()
  171.         self.button2 = gtk.Button(label = None, stock = 'gtk-no', use_underline = False)
  172.         self.button2.connect('clicked', self.on_button2_clicked, None)
  173.         self.button2.show()
  174.         buttonbox = gtk.HButtonBox()
  175.         buttonbox.set_layout(gtk.BUTTONBOX_END)
  176.         buttonbox.set_spacing(10)
  177.         buttonbox.pack_start(self.button2)
  178.         buttonbox.pack_start(self.button1)
  179.         buttonbox.show()
  180.         vbox.pack_start(self.label)
  181.         vbox.pack_start(buttonbox)
  182.         self.window.add(vbox)
  183.         self.label.show()
  184.         vbox.show()
  185.  
  186.     
  187.     def on_button1_clicked(self, widget, data = None):
  188.         global clean
  189.         self.window.hide()
  190.         self.conf = get_xkit_service()
  191.         if not self.conf:
  192.             sys.exit(1)
  193.         
  194.         status = self.conf.setVirtual(self.resolution)
  195.         if status == True:
  196.             clean = True
  197.             gtk.main_quit()
  198.         else:
  199.             gtk.main_quit()
  200.  
  201.     
  202.     def on_button2_clicked(self, widget, data = None):
  203.         self.window.hide()
  204.         gtk.main_quit()
  205.  
  206.     
  207.     def on_delete_event(self, widget, event, data = None):
  208.         return False
  209.  
  210.     
  211.     def on_destroy(self, widget, data = None):
  212.         gtk.main_quit()
  213.  
  214.     
  215.     def show(self):
  216.         self.window.show()
  217.  
  218.  
  219. if __name__ == '__main__':
  220.     if len(sys.argv) > 1:
  221.         for param in sys.argv[1:]:
  222.             if 'x' not in param:
  223.                 sys.exit(0)
  224.                 continue
  225.         
  226.     else:
  227.         sys.exit(0)
  228.     res = sys.argv[1]
  229.     res = res.strip().split('x')
  230.     if checkVirtual(res):
  231.         sys.exit(0)
  232.     
  233.     window = BootWindow(res)
  234.     window.show()
  235.     gtk.main()
  236.     if not clean:
  237.         sys.exit(1)
  238.     
  239.  
  240.